//
//  GCW_SongMuneViewController.m
//  MooMusic
//
//  Created by 管晨雯 on 15/10/22.
//  Copyright © 2015年 wjy. All rights reserved.
//

#import "GCW_SongMuneViewController.h"
#import "GCW_SongMuneCell.h"
#import "PrefixHeader.pch"
#import "NetWork.h"
#import "GCW_SongsMuneModel.h"
// 导入头文件
#import "LTY_FilmAndSongsViewController.h"

@interface GCW_SongMuneViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *songMuneCollectionView;
@property (nonatomic, strong) NSMutableArray *songMuneArr;
@property (nonatomic, strong) NSMutableArray *songMuneNewArr;

@property (nonatomic, assign) NSInteger page; /**< 控制页数*/

@end


/* cell的重用标志 */
static NSString *const  cellReuse = @"reuse";
@implementation GCW_SongMuneViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.songMuneArr  = [NSMutableArray array];
        self.songMuneNewArr = [NSMutableArray array];
        self.page = 1;
    }
    
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    /* 最热按钮 */
    UIButton *hotButton = [UIButton buttonWithType:UIButtonTypeCustom];
    hotButton.frame = CGRectMake(210 * WIDTH, 10 * HEIGH, 70 * WIDTH , 30 * HEIGH);
//    hotButton.backgroundColor = [UIColor brownColor];
    [hotButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [hotButton setTitle:@"最热" forState:UIControlStateNormal];
    [hotButton addTarget:self action:@selector(hotClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:hotButton];
    
    
    /* 分割线 */
    UIView *view = [[UIView alloc]init];
    view.frame = CGRectMake(290 * WIDTH, 10 * HEIGH, 2 * WIDTH, 20 * HEIGH);
    view.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:view];
    
    /* 最新按钮 */
    UIButton *newstButton =[UIButton buttonWithType:UIButtonTypeCustom];
    newstButton.frame = CGRectMake(300 * WIDTH, 10 * HEIGH, 66 * WIDTH, 30 * HEIGH);
//    newstButton.backgroundColor = [UIColor brownColor];
    [newstButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [newstButton setTitle:@"最新" forState:UIControlStateNormal];
    [newstButton addTarget:self action:@selector(newClicked:) forControlEvents:UIControlEventTouchUpInside];
    newstButton.tag = 1000;
    [self.view addSubview:newstButton];
    
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"歌单广场";
    
    
    [self creatUICollectionView];
    [self getData];
    [self addFooterCollection];
}


#pragma mark --- 创建UICollectionView
- (void)creatUICollectionView
{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    /* 设置方块的大小 */
    flowLayout.itemSize = CGSizeMake(160 * WIDTH, 200 *HEIGH);
    
    /* 设置方块的列间距  */
    flowLayout.minimumInteritemSpacing = 4 * WIDTH;
    
    /* 设置最小行间距 */
    flowLayout.minimumLineSpacing = 5 * HEIGH;
    flowLayout.sectionInset = UIEdgeInsetsMake(10 , 16 * HEIGH, 6 , 16 * HEIGH);
    
//    /* 设置头部高度 */
//    flowLayout.headerReferenceSize = CGSizeMake(0, 98 * HEIGH);
//    
    
    /* 集合视图 */
    self.songMuneCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 50 * HEIGH, self.view.frame.size.width, self.view.frame.size.height - 110) collectionViewLayout:flowLayout];
    

    self.songMuneCollectionView.delegate = self;
    self.songMuneCollectionView.dataSource = self;
    
    /* 设置常用属性 */
    self.songMuneCollectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.songMuneCollectionView];
    
#pragma mark -- 注册cell的重用标志
    
    [self.songMuneCollectionView registerClass:[GCW_SongMuneCell class] forCellWithReuseIdentifier:cellReuse];
}

#pragma mark --- 获取数据
- (void)getData
{
    
    NSString *urlString = [NSString stringWithFormat:songsMunePageUrl@"%ld"songMunePageUrlEnd, _page];
    
    NSLog(@"%@", urlString);
    
    [NetWork networkGETRequestWithURL:urlString  Paramater:nil ResultBlock:^(id result) {
        
        NSMutableDictionary *mDic = (NSMutableDictionary *)result;
        
        NSMutableArray *dataArr = [mDic objectForKey:@"data"];
        
        for (NSMutableDictionary *dic in dataArr) {
            GCW_SongsMuneModel *model = [[GCW_SongsMuneModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.songMuneArr addObject:model];
        }
        
        if (self.songMuneArr.count > 0) {
            [self.songMuneCollectionView reloadData];
        }
        
        
        // 下拉刷新停止
        [self.songMuneCollectionView footerEndRefreshing];
       
        
    }];
}

#pragma mark --上拉加载collectionview数据更多
- (void)addFooterCollection
{
    //    __unsafe_unretained typeof(self) vc = self;
    // __block: 表示在 block 中的修改对于 block 外也是有效地
    __block GCW_SongMuneViewController *vc = self;
    
    // 添加上拉刷新尾部控件
    [self.songMuneCollectionView addFooterWithCallback:^{
        // 进入刷新状态就会回调这个Block
        
        NSLog(@"上拉加载更多，开始网络请求更多数据");
          _page += 1;
        vc.isUpLoading = YES;
        //用afn加载更多的数据
        [vc getData];
        
    }];
}


#pragma mark --- 最新按钮的点击事件
- (void)newClicked: (UIButton *)button
{
    [self.songMuneArr removeAllObjects];
    
    NSString *urlString1 = [NSString stringWithFormat:songsMuneNewPageUrl@"%ld"songsMuneNewPageUrlEnd, _page];
    
    [NetWork networkGETRequestWithURL:urlString1  Paramater:nil ResultBlock:^(id result) {
        
        NSMutableDictionary *mDic = (NSMutableDictionary *)result;
        
        NSMutableArray *dataArr = [mDic objectForKey:@"data"];
        
        
        for (NSMutableDictionary *dic in dataArr) {
            GCW_SongsMuneModel *model = [[GCW_SongsMuneModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.songMuneArr addObject:model];
        }
        
        if (self.songMuneArr.count > 0) {
            [self.songMuneCollectionView reloadData];
        }
    }];
    
}


#pragma mark --- 最热按钮的点击事件
-(void)hotClicked: (UIButton *)button
{
    
    [self.songMuneArr removeAllObjects];
    
    [NetWork networkGETRequestWithURL:songsMunePageUrl  Paramater:nil ResultBlock:^(id result) {
        
        NSMutableDictionary *mDic = (NSMutableDictionary *)result;
        
        NSMutableArray *dataArr = [mDic objectForKey:@"data"];
        
        for (NSMutableDictionary *dic in dataArr) {
            GCW_SongsMuneModel *model = [[GCW_SongsMuneModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.songMuneArr addObject:model];
        }
        
        if (self.songMuneArr.count > 0) {
            [self.songMuneCollectionView reloadData];
        }
        
        
    }];

}

#pragma mark -- 实现协议方法

/* 返回的items的数量 */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.songMuneArr.count > 0 ) {
        return self.songMuneArr.count;
    }else
    {
        return 0;
    }
    
    
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
             GCW_SongMuneCell *cell = [collectionView  dequeueReusableCellWithReuseIdentifier:cellReuse forIndexPath:indexPath];
  
    /* 赋值 */
    if (self.songMuneArr.count  >  0 ) {
        
        GCW_SongsMuneModel *model = [self.songMuneArr objectAtIndex:indexPath.row];
        cell.songsMuneModel = model;
        
    }
        return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    LTY_FilmAndSongsViewController *filmVC = [[LTY_FilmAndSongsViewController alloc]init];
    
    GCW_SongsMuneModel *model = [self.songMuneArr objectAtIndex:indexPath.row];
    NSString *string = [NSString stringWithFormat:@"%@",model.quan_id];
    NSString *stringUrl = [NSString stringWithFormat:HotSongString, string];
    filmVC.DayString = stringUrl;
    filmVC.nameString = model.title;
    filmVC.descString = model.desc;
    [self.navigationController pushViewController:filmVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
